package com.example.wanbaqian.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.AddMoneyBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.CameraUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.moxie.client.MainActivity;
import com.moxie.client.model.MxParam;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/11.
 * 提额资料填写
 */

public class AddMoneyActivity extends BaseActivity implements HttpCallback, BaseActivity.PermissionListeren {

    private static final String TAG = "AddMoneyActivity";
    private static final int FCZ = 0X11111;
    private static final int CLXSZ = 0X22222;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.am_money)
    EditText amMoney;
    @BindView(R.id.am_gjjTv)
    TextView amGjjTv;
    @BindView(R.id.am_sbTv)
    TextView amSbTv;
    @BindView(R.id.am_xykTv)
    TextView amXykTv;
    private String imagePath = "";
    private boolean isFCZ = true;
    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.iv_two)
    ImageView ivTwo;

    @Override
    protected int getLayout() {
        return R.layout.activity_addmoney;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("提额");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        initDataView((AddMoneyBean) getIntent().getExtras().getSerializable("bean"));
    }

    /**
     * 初始化数据填充
     *
     * @param bean
     */
    private void initDataView(AddMoneyBean bean) {
        amMoney.setText(bean.getPromote_credit());
        amGjjTv.setText(bean.getFund_fill_flag() == 0 ? "未完善(选填)" : "已完善");
        amGjjTv.setTextColor(bean.getFund_fill_flag() == 0 ? ContextCompat.getColor(mContext, R.color.colorTextGray) : ContextCompat.getColor(mContext, R.color.colorBackground));

        amSbTv.setText(bean.getSecurity_fill_flag() == 0 ? "未完善(选填)" : "已完善");
        amSbTv.setTextColor(bean.getSecurity_fill_flag() == 0 ? ContextCompat.getColor(mContext, R.color.colorTextGray) : ContextCompat.getColor(mContext, R.color.colorBackground));

        amXykTv.setText(bean.getEmail_fill_flag() == 0 ? "未完善(选填)" : "已完善");
        amXykTv.setTextColor(bean.getEmail_fill_flag() == 0 ? ContextCompat.getColor(mContext, R.color.colorTextGray) : ContextCompat.getColor(mContext, R.color.colorBackground));

        Glide.with(mContext).load(bean.getHouse_url()).into(ivOne);
        Glide.with(mContext).load(bean.getCar_url()).into(ivTwo);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CameraUtils.REQ_TAKE_PHOTO:
                    if (isFCZ) {
                        Glide.with(mContext).load(imagePath).into(ivOne);
                    } else {
                        Glide.with(mContext).load(imagePath).into(ivTwo);
                    }
                    HttpUtils.getInstance().postFile(HttpURL.IMAGE_UPLOAD, mContext,
                            isFCZ ? CommonValue.house : CommonValue.car, new File(imagePath),
                            isFCZ ? CommonValue.REQUEST_CODE1 : CommonValue.REQUEST_CODE2, AddMoneyActivity.this);
                    break;
                case CommonValue.REQUEST_CODE1:
                case CommonValue.REQUEST_CODE2:
                case CommonValue.REQUEST_CODE3:
                    String result = data.getExtras().getString("result");
                    if (JsonUtils.getInstance().getInt(result, "code") == 1) {
                        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.THIRD_RESPONSE, mContext,
                                new String[]{"accessid", "response_type", "response_info"},
                                new String[]{SPUtils.getAccessid(mContext),
                                        requestCode == CommonValue.REQUEST_CODE1 ? CommonValue.fund :
                                                requestCode == CommonValue.REQUEST_CODE2 ? CommonValue.security : CommonValue.email, result}),
                                "", requestCode == CommonValue.REQUEST_CODE1 ? CommonValue.REQUEST_CODE4 :
                                        requestCode == CommonValue.REQUEST_CODE2 ? CommonValue.REQUEST_CODE5 : CommonValue.REQUEST_CODE6, this);
                    } else {
                        showToast("数据获取失败");
                    }
            }
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                showToast("房产证上传成功");
                break;
            case CommonValue.REQUEST_CODE2:
                showToast("车辆行驶证上传成功");
                break;
            case CommonValue.REQUEST_CODE3:
                openActivity(AddMoneyoneActivity.class);
                finish();
                break;
            case CommonValue.REQUEST_CODE4:
                amGjjTv.setText("已完善");
                amGjjTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorBackground));
                break;
            case CommonValue.REQUEST_CODE5:
                amSbTv.setText("已完善");
                amSbTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorBackground));
                break;
            case CommonValue.REQUEST_CODE6:
                amXykTv.setText("已完善");
                amXykTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorBackground));
                break;

        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    @Override
    public void onGranted(int requestCode) {
        isFCZ = requestCode == FCZ ? true : false;
        imagePath = CameraUtils.takePhoto(this);
        Log.i(TAG, "onGranted: " + imagePath);
    }

    @Override
    public void onDenied(int requestCode, List<String> deniedPermissions, List<String> noReminderPermissions) {
        showToast("请同意拍照权限");
    }


    @OnClick({R.id.am_gjjLayout, R.id.am_sbLayout, R.id.am_xykLayout, R.id.iv_one, R.id.iv_two, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.am_gjjLayout:
            case R.id.am_sbLayout:
            case R.id.am_xykLayout:
                Bundle bundle = new Bundle();
                MxParam mxParam = new MxParam();
                mxParam.setUserId(SPUtils.getUserInfo(mContext).getUser_id() + "");
//                mxParam.setApiKey(CommonValue.TDAUTHKEY);
                //2017年9月8日10:25:56修改为自己的魔蝎key
                mxParam.setApiKey(CommonValue.MX_API_KEY);
                mxParam.setBannerBgColor("#04C9D4"); // SDK里Banner的背景色
                mxParam.setBannerTxtColor("#FFFFFF"); // SDK里Banner的字体颜色
                mxParam.setFunction(view.getId() == R.id.am_gjjLayout ? MxParam.PARAM_FUNCTION_FUND :
                        view.getId() == R.id.am_sbLayout ? MxParam.PARAM_FUNCTION_SECURITY : MxParam.PARAM_FUNCTION_ONLINEBANK);
                if (view.getId() == R.id.am_xykLayout) {
                    mxParam.setLoginType(MxParam.PARAM_ITEM_TYPE_CREDITCARD);
                }
                bundle.putParcelable("param", mxParam);
                openActivityForResult(MainActivity.class, bundle, view.getId() == R.id.am_gjjLayout ? CommonValue.REQUEST_CODE1 :
                        view.getId() == R.id.am_sbLayout ? CommonValue.REQUEST_CODE2 : CommonValue.REQUEST_CODE3);
                break;
            case R.id.iv_one:
                requestPermission(FCZ, this, Manifest.permission.CAMERA);
                break;
            case R.id.iv_two:
                requestPermission(CLXSZ, this, Manifest.permission.CAMERA);
                break;
            case R.id.btn_ok:
                String money = amMoney.getText().toString();
                if (TextUtils.isEmpty(money) || Integer.parseInt(money) <= 0) {
                    showToast("请填写提额金额");
                    return;
                } else {
                    HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.QUOTA_APPLY, mContext
                            , new String[]{"accessid", "apply_money"}
                            , new String[]{SPUtils.getAccessid(mContext), money})
                            , mContext, null, CommonValue.REQUEST_CODE3, this);
                }
                break;
        }
    }
}